<?php

/*============================================
Classe McsControl
============================================*/

class McsControl extends ModelObject{

	protected static $cached_object_list= array();
	protected $MEAS_NAME;
	protected $RESSOURCES;
	protected $LAUNCH_STATEMENT;
	protected $SIMU_EXEC_CTRLE;

	protected function __construct( $MEAS_NAME,$RESSOURCES,$LAUNCH_STATEMENT,$SIMU_EXEC_CTRLE){
		try{
			
			$this->MEAS_NAME = $MEAS_NAME;
			$this->RESSOURCES = $RESSOURCES;
			$this->LAUNCH_STATEMENT = $LAUNCH_STATEMENT;
			$this->SIMU_EXEC_CTRLE = $SIMU_EXEC_CTRLE;
		}
		catch(Excpetion $e){
			throw new Exception("La construction de l'objet de type get_class($this) a échouée",0,$e);
		}
	}

	public function getMEASNAME(){
		return $this->MEAS_NAME;
	}

	public function getRESSOURCES(){
		return $this->RESSOURCES;
	}

	public function getLAUNCHSTATEMENT(){
		return $this->LAUNCH_STATEMENT;
	}

	public function getSIMUEXECCTRLE(){
		return $this->SIMU_EXEC_CTRLE;
	}

	public function setRESSOURCES( $RESSOURCES){
		try{
			$temp = $this->RESSOURCES;
			if($this->RESSOURCES !== $RESSOURCES){
				$this->RESSOURCES = $RESSOURCES;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->RESSOURCES = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setRESSOURCES() a échouée",0,$e);
		}
	}

	public function setLAUNCHSTATEMENT( $LAUNCH_STATEMENT){
		try{
			$temp = $this->LAUNCH_STATEMENT;
			if($this->LAUNCH_STATEMENT !== $LAUNCH_STATEMENT){
				$this->LAUNCH_STATEMENT = $LAUNCH_STATEMENT;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->LAUNCH_STATEMENT = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setLAUNCHSTATEMENT() a échouée",0,$e);
		}
	}

	public function setSIMUEXECCTRLE( $SIMU_EXEC_CTRLE){
		try{
			$temp = $this->SIMU_EXEC_CTRLE;
			if($this->SIMU_EXEC_CTRLE !== $SIMU_EXEC_CTRLE){
				$this->SIMU_EXEC_CTRLE = $SIMU_EXEC_CTRLE;
				$this->update();
			}
		}
		catch(Exception $e){
			$this->SIMU_EXEC_CTRLE = $temp;
			throw new Exception("La modification de l'attribut par l'appel à la méthode setSIMUEXECCTRLE() a échouée",0,$e);
		}
	}

	public static function create( $MEAS_NAME,$RESSOURCES,$LAUNCH_STATEMENT,$SIMU_EXEC_CTRLE){
		try{
			$mcsControl = new McsControl($MEAS_NAME, $RESSOURCES, $LAUNCH_STATEMENT, $SIMU_EXEC_CTRLE);
			$mcsControl->insert();
			McsControl::cacheObject($mcsControl);
			return $mcsControl;
		}
		catch(Excpetion $e){
			throw new Exception("La création de l'objet de type McsControl a échouée",0,$e);
		}
	}

	public static function get( $MEAS_NAME){
		$dbh = SingletonPDO::getInstance();
		try{
			if(is_null($MEAS_NAME) || $MEAS_NAME === "") {
				return null;
			}
			if(McsControl::existsInCache($MEAS_NAME)) {
				return McsControl::getFromCache($MEAS_NAME);
			}
			$dbh->beginTransaction();
			$sql= 'SELECT * FROM `mcs_control` WHERE `MEAS_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$MEAS_NAME));
			if ($row = $stmt->fetch()){
				$mcsControl = new McsControl( $row['MEAS_NAME'],$row['RESSOURCES'],$row['LAUNCH_STATEMENT'],$row['SIMU_EXEC_CTRLE']);
				$mcsControl->id =  $row['MEAS_NAME'];
				$dbh->commit();
				McsControl::cacheObject($mcsControl);
				return $mcsControl;
			}
			else {
				$dbh->commit();
				return null;
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récupération de l'objet ayant l'id ". $MEAS_NAME . " a échouée",0,$e);
		}
	}

	protected function update(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql='UPDATE `mcs_control` SET `RESSOURCES` = :RESSOURCES, `LAUNCH_STATEMENT` = :LAUNCH_STATEMENT, `SIMU_EXEC_CTRLE` = :SIMU_EXEC_CTRLE WHERE `MEAS_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':RESSOURCES'=>$this->RESSOURCES, ':LAUNCH_STATEMENT'=>$this->LAUNCH_STATEMENT, ':SIMU_EXEC_CTRLE'=>$this->SIMU_EXEC_CTRLE, ':id'=>$this->id));
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La modification de l'objet de type ".get_class($this)." ayant l'id ". $this->id . " a échouée",0,$e);
		}
	}

	public function delete(){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			if (is_null($this->id)) {
				throw new Exception("Cet objet de type ".get_class($this)." n'a pas encore été enregistré dans la base");
			}
			$sql = 'DELETE FROM `mcs_control` WHERE `MEAS_NAME` = :id';
			$stmt = $dbh->prepare($sql);
			$stmt->execute(array(':id'=>$this->id));
			McsControl::removeObjectFromCache($this->id);
			$this->id = null;
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de l'objet de type ".get_class($this)." ayant l'id ". $this->id. " a échouée",0,$e);
		}
	}

	protected function insert(){
		$dbh = SingletonPDO::getInstance();
		try{
			if (is_null($this->id)) {
				$dbh->beginTransaction();
				$sql='INSERT INTO `mcs_control` ( `RESSOURCES`, `LAUNCH_STATEMENT`, `SIMU_EXEC_CTRLE`) VALUES (:RESSOURCES, :LAUNCH_STATEMENT, :SIMU_EXEC_CTRLE)';
				$stmt = $dbh->prepare($sql);
				$stmt->execute(array(':RESSOURCES'=>$this->RESSOURCES, ':LAUNCH_STATEMENT'=>$this->LAUNCH_STATEMENT, ':SIMU_EXEC_CTRLE'=>$this->SIMU_EXEC_CTRLE));
				$this->id = $dbh->lastInsertId();
				$dbh->commit();
			}
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("L'insertion de l'objet de type ".get_class($this)." a échouée",0,$e);
		}
	}

	public static function getAll( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT * FROM `mcs_control`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			while ($row = $stmt->fetch()) {
				$obj = null;
				if(McsControl::existsInCache($row['MEAS_NAME'])){
					$obj = McsControl::getFromCache($row['MEAS_NAME']);
				}
				else{
					$obj = new McsControl( $row['MEAS_NAME'],$row['RESSOURCES'],$row['LAUNCH_STATEMENT'],$row['SIMU_EXEC_CTRLE']);
				}
				if(!is_null($obj)){
					$obj->id = $row['MEAS_NAME'];
					McsControl::cacheObject($obj);
					$objs[] = $obj;
				}
			}
			$dbh->commit();
			return $objs;
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type McsControl a échouée",0,$e);
		}
	}

	public static function getCount( $search_params = array(),$options = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql = 'SELECT count(*) as count FROM `mcs_control`';
			$sql .= self::generateWhereClause($search_params);
			$sql .= self::generateOptionClause($options);
			$stmt = $dbh->prepare($sql);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$stmt->execute($array);
			$count = $stmt->fetch();
			$dbh->commit();
			return $count['count'];
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La récuperation de la liste d'objets de type McsControl a échouée",0,$e);
		}
	}

	public static function deleteAll( $search_params = array()){
		$dbh = SingletonPDO::getInstance();
		try{
			$dbh->beginTransaction();
			$objs = array();
			$sql_delete = 'DELETE FROM `mcs_control`';
			$sql_select = 'SELECT MEAS_NAME FROM `mcs_control`';
			$sql_delete .= self::generateWhereClause($search_params);
			$sql_select .= self::generateWhereClause($search_params);
			$stmt_delete = $dbh->prepare($sql_delete);
			$stmt_select = $dbh->prepare($sql_select);
			$array = array();
			$cpt = 0;
			foreach($search_params as $ind => $search_param){
				//ajouter un parametre au tableau
				if($search_param['operator'] === 'BETWEEN' && is_array($search_param['value']) && count($search_param['value']) == 2 ){
					$array[':'.$search_param['field'].$cpt.'_0'] = $search_param['value'][0];
					$array[':'.$search_param['field'].$cpt.'_1'] = $search_param['value'][1];
				}
				else if($search_param['operator'] === 'IN' && is_array($search_param['value']) && count($search_param['value']) > 0){
					$i = 0;
					foreach($search_param['value'] as $key => $val){
						$array[':'.$search_param['field'].$cpt.'_'.$i] = $val;
						$i++;
					}
					if($i == 0){
						throw new Exception("L'opérateur IN doit comporter au moins une valeur");
					}
				}
				else if ($search_param['operator'] === 'IS NULL' || $search_param['operator'] === 'IS NOT NULL'){
					continue;
				}
				else{
					$array[':'.$search_param['field'].$cpt] = $search_param['value'];
				}
				$cpt++;
			}
			$var_select = $stmt_select->execute($array);
			$tab_to_delete_objects = $stmt_select->fetchAll();
			if(sizeof($tab_to_delete_objects) > 0){
				$var_delete = $stmt_delete->execute($array);
				foreach($tab_to_delete_objects as $ind => $row){
					$existing_object = McsControl::getObjectFromCache($row['MEAS_NAME']);
					if(!is_null($existing_object)){
						$existing_object->id = null;
						McsControl::removeObjectFromCache($row['MEAS_NAME']);
					}
				}
			}
			$dbh->commit();
		}
		catch(Exception $e) {
			$dbh->rollBack();
			throw new Exception("La suppression de la liste d'objets de type McsControl a échouée",0,$e);
		}
	}

}